//
// Created by Szper on 2018/4/16.
//
#include "Token.h"
char* Token::getRes() {
    return &this->res[0];
}
//read resource file
void Token::readIfile() {
    cout << this->ifilename << endl;
    if ((this->ifile=fopen(this->ifilename.c_str(), "r")) == NULL){
        printf("can't open this file\n");
        exit(0);
    }
    this->res[resLength] = fgetc(this->ifile);
    while(res[resLength] != '$'){
        ++resLength;
        res[resLength] = fgetc(ifile);
    }
    res[resLength] = '\0';
}
//write the explain to result.txt
void Token::writeOfile() {
    if((this->ofile = fopen("result.txt", "w")) == NULL){
        printf("can't write to this file\n");
        exit(0);
    }
}
//destroy the file point ifile, ofile
void Token::destroy() {
    if(!this->ifile)
        fclose(this->ifile);
    if(!this->ofile)
        fclose(this->ofile);
    this->resLength = 0;
}
//get resource's length
int Token::getResLength() {
    return this->resLength;
}
SolveCenter* Token::getSolve() {
    return solve;
}
Token::Token(char* ifilename, char* ofilename) {
    solve = new SolveCenter();
    this->ifilename = ifilename;
    this->ofilename = ofilename;
    this->resLength = 0;
    this->syn = -1;
}
void Token::Solve() {
    this->solve->preCompile(this->res, this->resLength);
    /*this->writeOfile();
    while(this->syn != 0){
        this->solve->Analysis(this->syn, this->res, this->tocken, this->resLength);
    }*/

}
/*void Token::ClearToken() {
    for(int i = 0; i < 21; i++){
        this->tocken[i] = '\0';
    }
}*/
Token::~Token(){
    this->destroy();
}